package com.yanqu.road.dao.impl.activity.commonsupport;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.commonsupport.CommonUnionSupportData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CommonUnionSupportDataDaoImpl extends TempDataObjectDao<CommonUnionSupportData> {

    public static String getCommonUnionSupportDataSql = "select * from `t_c_common_union_support` where `activityId`=? and `groupId`=?";
    public Map<String, CommonUnionSupportData> getCommonUnionSupportData(int activityId,int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, CommonUnionSupportData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCommonUnionSupportDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                CommonUnionSupportData data = parse(rs);
                map.put(data.getUnionUid(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateCommonUnionSupportDataSql = "update `t_c_common_union_support` set hotCount=? where activityId=? and groupId=? and unionUid=?";
    @Override
    protected boolean updateData(CommonUnionSupportData commonUnionSupportData) {
        commonUnionSupportData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, commonUnionSupportData.getHotCount()));
        para.put(i++, new DbParameter(Types.INTEGER, commonUnionSupportData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, commonUnionSupportData.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, commonUnionSupportData.getUnionUid()));
        if (getDb().executeNoneQuery(updateCommonUnionSupportDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCommonUnionSupportDataSql = "insert into `t_c_common_union_support`(activityId,groupId,unionUid,hotCount) values (?,?,?,?)";
    @Override
    protected boolean addData(CommonUnionSupportData commonUnionSupportData) {
        commonUnionSupportData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, commonUnionSupportData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, commonUnionSupportData.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, commonUnionSupportData.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, commonUnionSupportData.getHotCount()));
        if (getDb().executeNoneQuery(insertCommonUnionSupportDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CommonUnionSupportData parseData(ResultSet rs) throws SQLException {
        CommonUnionSupportData commonUnionSupportData = new CommonUnionSupportData();
        commonUnionSupportData.setActivityId(rs.getInt("activityId"));
        commonUnionSupportData.setGroupId(rs.getInt("groupId"));
        commonUnionSupportData.setUnionUid(rs.getString("unionUid"));
        commonUnionSupportData.setHotCount(rs.getInt("hotCount"));

        return commonUnionSupportData;
    }
}
